package weather;

import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

/**
 *
 * @author Sebas
 */
public class ForecastDisplay implements Observer, DisplayElement {
    ArrayList<Float> temps;
    private int k;
    Observable observable;
    private float forecast;
    
    public ForecastDisplay(int k, Observable observable){
        this.k=k;
        this.observable = observable;
        observable.addObserver(this);
    }
    
    public void update(Observable obs, Object arg) {
        if (obs instanceof WeatherData) {
            WeatherData weather = (WeatherData) obs;
            this.temps.add(weather.getTemperature());
            display();
        }
    }
    
    private boolean forecast(){
        if(temps.size()>=k){
            float total =0;
            for(int i=temps.size(); i>=k;i--){
                total=temps.get(i) + total;
            }
            setForecast(total/k);
            return true;
        }else {
            return false;
        }
    }
    
    public void display() {
        if(forecast()){
            System.out.println("**The temperature forecast of last "+k+" set of data is: "+getForecast());
        }else{
            System.out.println("There is not enough information to do a forecast.");
        }
    }

    public void setForecast(float forecast) {
        this.forecast = forecast;
    }

    public float getForecast() {
        return forecast;
    }
    
}
